/**
 * 自定义箭头
 */


class CustomArrow extends AreaPlotBase {

  constructor(points) {
    super(points);
    this.headRightPnt;
    this.neckPnt;

    this.headHeightFactor = 0.15;
    this.headWidthFactor = 0.4;
    this.neckHeightFactor = 0.75;
    this.neckWidthFactor = 0.15;
  }


  get_minPointCount() {
    return 2;
  }

  create() {
    let n = this.points.length;
    if (n >= 3 && this.points[n - 1] != this.points[n - 2]) {
      // 箭尾点
      let tailLeftPoint = this.points[0];
      let tailRightPoint = this.points[1];
      let tailMidPnt = super.getMidPoint(tailLeftPoint, tailRightPoint);

      let pnts = [];
      pnts.push(tailMidPnt);
      pnts = pnts.concat(this.points.slice(2, this.points.length));

      // 获取组成箭头的五个点（顺时针）
      let arrowPoints = super.getArrowHeadPoints(pnts, this.headHeightFactor, this.headWidthFactor, this.neckHeightFactor, this.neckWidthFactor);
      // 箭颈左右点
      let neckLeftPoint = arrowPoints[0];
      let neckRightPoint = arrowPoints[4];
      // 获取箭身(顺时针）
      let tailWidthFactor = super.distance(tailLeftPoint, tailRightPoint) / 2 / super.getBaseLength(pnts) / 2;
      let bodyPoints = super.getArrowBodyPoints(pnts, neckLeftPoint, neckRightPoint, tailWidthFactor);

      // 将箭颈点、箭身点、箭尾点合并
      n = bodyPoints.length;
      let lPoints = bodyPoints.slice(0, n / 2);
      let rPoints = bodyPoints.slice(n / 2, n);
      // 合并箭颈点
      lPoints.push(neckLeftPoint);
      rPoints.push(neckRightPoint);
      // 合并箭尾点
      lPoints.reverse();
      lPoints.push(tailLeftPoint);
      rPoints.reverse();
      rPoints.push(tailRightPoint);

      lPoints = super.getBSplinePoints(lPoints);
      rPoints = super.getBSplinePoints(rPoints);
      lPoints.reverse();
      lPoints = lPoints.concat(arrowPoints);
      lPoints = lPoints.concat(rPoints);

      let pc3 = []
      for (let j = 0; j < lPoints.length; j++) {
        pc3.push(lPoints[j]);
        //         Debugger.Log(1, "pnt", lPoints[j].ToString());
      }
      pc3.push(lPoints[0]);


      if (this.PartCount > 0) {
        this.SetEmpty();
      }
      this.AddPart(pc3);
    }
  }

  updateControlPoint(point, index) {
    if (index < this.get_pointCount()) {
      this.points[index] = point;
    }
    else {
      if (index == super.get_pointCount())
        this.headRightPnt = point;
      else if (index == this.get_pointCount() + 1)
        this.neckPnt = point;

      super.calculateFactors();
    }
    this.createControlPoints();
    this.create();
  }

  finishDraw() {
    this.createControlPoints();
    super.finishDraw();
  }

  getControlPoints() {
    let pnts = [].concat(this.points);
    //pnts.AddRange(this.points);
    ///	var pnts:Array=this.points.slice();
    pnts.push(this.headRightPnt);
    pnts.push(this.neckPnt);
    return pnts;
  }

  setControlPoints(value) {
    if (value.length - 2 == this.points.length) {
      this.points = value.slice(0, super.get_pointCount());
      this.createControlPoints();
      this.create();
    }
  }

  createControlPoints() {
    // 箭尾点
    //if (this.points.Count < 2)
    //    return;
    let tailLeftPoint = this.points[0];
    let tailRightPoint = this.points[1];
    let tailMidPnt = super.getMidPoint(tailLeftPoint, tailRightPoint);

    let pnts = [];
    pnts.push(tailMidPnt);
    pnts = pnts.concat(this.points.slice(2, this.points.length));

    let allLen = super.getBaseLength(pnts);
    let headHeight = allLen * headHeightFactor;
    let headWidth = headHeight * headWidthFactor;
    let n = pnts.Count;
    let headPnt = pnts[n - 1];
    // 如果最后两个点之间的距离小于headHeight，则使用这两个点之间的距离作为headHeight
    let len = super.distance(headPnt, pnts[n - 2]);
    headHeight = headHeight > len ? len : headHeight;
    let neckHeight = headHeight * neckHeightFactor;
    // 计算箭颈点和箭头左边的点
    let headEndPt = super.getThirdPoint(pnts[n - 2], headPnt, 0, headHeight, LEFT_SIDE);
    let neckEndPt = super.getThirdPoint(pnts[n - 2], headPnt, 0, neckHeight, LEFT_SIDE);
    let headRightPt = super.getThirdPoint(headPnt, headEndPt, Math.PI * 1.5, headWidth, LEFT_SIDE);
    this.headRightPnt = headRightPt;
    this.neckPnt = neckEndPt;
  }

  calculateFactors() {
    // 箭尾点
    let tailLeftPoint = this.points[0];
    let tailRightPoint = this.points[1];
    let tailMidPnt = super.getMidPoint(tailLeftPoint, tailRightPoint);

    let pnts = [];
    pnts.push(tailMidPnt);
    pnts = pnts.concat(this.points.slice(2, this.points.length));

    let headPnt = this.points[this.points.length - 1];
    let headLeftPt = this.headRightPnt;
    let neckPt = this.neckPnt;
    let allLen = super.getBaseLength(pnts);
    //箭头斜边长度
    let len = super.distance(headPnt, headLeftPt);
    let angle = super.getAngleOfThreePoints(headLeftPt, headPnt, this.points[this.points.length - 2]);
    let headHeight = len * Math.cos(angle);
    this.headHeightFactor = headHeight / allLen;
    let headWidth = len * Math.sin(angle);
    this.headWidthFactor = headWidth / headHeight;
    //计算颈宽系数
    len = super.distance(headPnt, neckPt);
    angle = super.getAngleOfThreePoints(this.points[this.points.length - 2], headPnt, neckPt);
    let neckHeight = len * Math.Cos(angle);
    //控制箭颈高度保持在合理范围(箭高的0.2到1倍之间)
    let minNeckHeight = headHeight * 0.2;
    if (neckHeight < minNeckHeight)
      neckHeight = minNeckHeight;
    if (neckHeight > headHeight)
      neckHeight = headHeight;
    this.neckHeightFactor = neckHeight / headHeight;
  }
}
